//
//  WCNCareRecordCommentListVC.m
//  WCN
//
//  Created by wsj2015 on 16/4/6.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNCareRecordCommentListVC.h"
#import "WCNRecordCommentModel.h"
#import "WCNRecordCommentCell.h"
#import "WCNMulTextEditVC.h"
#import "UITableView+FDTemplateLayoutCell.h"

@interface WCNCareRecordCommentListVC ()


@property (nonatomic,  strong) NSMutableArray *dataArray;

@property (nonatomic,  strong) NSString *comment;
@property (nonatomic,  strong) UIButton *footerBtn;

@end

@implementation WCNCareRecordCommentListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self configUI];
    [self.tableView.header beginRefreshing];
}



- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
- (UIButton *)footerBtn{
    if (!_footerBtn) {
        _footerBtn =[UIButton buttonWithType:UIButtonTypeSystem];
        _footerBtn.frame =CGRectMake(30, kScreenHeight -35-65, kScreenWidth -60, 35);
        [_footerBtn setTitle:@"我来评论" forState:UIControlStateNormal];
        [_footerBtn setBackgroundColor:HexRGB_WSJ(84, 130, 255)];
        _footerBtn.layer.cornerRadius = 5;
        _footerBtn.layer.masksToBounds= YES;
        [_footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_footerBtn addTarget:self action:@selector(commentAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _footerBtn;
    
}
- (void)commentAction:(id)sender
{
    weakSelf_define;
    WCNMulTextEditVC *vc = [[WCNMulTextEditVC alloc] initWithType:WCNMulTextEditSave];
    vc.title = @"添加您的评论";
    vc.canEmpty = NO;
    vc.textViewPlaceholder = @"请填写评论";
    
    vc.textViewEmptyWarm = @"评论";
    vc.callback = ^(NSString *txt){
      
        weakSelf.comment = txt;
        [weakSelf addNewComment];
       
    };
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}
- (void)configUI{
    
    self.title = @"评论列表";
    defineWeakSelf;
    self.view.backgroundColor = kAppBg;
    self.tableView.frame= CGRectMake(0, 0, kScreenWidth, kScreenHeight-64-35);
    [self.tableView gzwLoading:^{
        [weakSelf.tableView.header beginRefreshing];
    }];
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf postCommentList];
    }];
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNRecordCommentCell" bundle:nil] forCellReuseIdentifier:@"WCNRecordCommentCell"];
    [self.view addSubview:self.footerBtn];
}

#pragma mark -评论列表
- (void)postCommentList
{
    self.tableView.loading = YES;
    defineWeakSelf;
    [[WCNetworkAccessManager sharedMg] POST:kCommetRecordList parameters:@{@"recordId":@(self.recordId),@"recordType":@(self.recordType)} success:^(NSDictionary *responseObject) {
        [weakSelf.dataArray removeAllObjects];
        NSArray *temAr =[WCNRecordCommentModel objectArrayWithKeyValuesArray:responseObject[@"data"]];
        if (temAr.count>0) {
            [weakSelf.dataArray addObjectsFromArray:temAr];
        }
      ;
        [weakSelf.tableView reloadData];
        
    } failure:nil netError:nil always:^{
        [weakSelf.tableView.header endRefreshing];
         weakSelf.tableView.loading =NO;
    }];
}


- (void)addNewComment{
//recordId: 记录ID
//recordType: 记录类型:1:伤口护理记录,2:PICC拔管记录,3:PICC维护记录
//comment: 评论内容
    
    if (!self.comment || [self.comment isEqualToString:@""]) {
        WSJSHOW(@"评论内容不能为空~");
        return;
    }
    
    CoreSVPLoading(@"上传中...", NO);
    defineWeakSelf;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"recordId"] = @(self.recordId) ;
    params[@"recordType"] = @(self.recordType) ;
    params[@"comment"] = self.comment ;
    [[WCNetworkAccessManager sharedMg] POST:kAddPiccRecordComment parameters:params success:^(NSDictionary *responseObject) {
        
        CoreSVPSuccess(responseObject[@"data"]);
        
        [weakSelf.tableView.header beginRefreshing];
        
    } failure:nil netError:nil always:^{
        [CoreSVP dismiss];
    }];

  
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNRecordCommentCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCNRecordCommentCell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    defineWeakSelf;
    return [tableView
     fd_heightForCellWithIdentifier:@"WCNRecordCommentCell" configuration:^(WCNRecordCommentCell *cell) {
         cell.model = weakSelf.dataArray[indexPath.row];
         
     }];;
}
@end
